#include "LightElement.hpp"

LightElement::LightElement(float x, float y, float angle, float arc, float length, LightElement::Type lightType, b2World* world) {
    this->arc = arc;
    this->length = length;
    this->lightType = lightType;

    b2BodyDef bodyDefinition;
    bodyDefinition.userData = this;
    bodyDefinition.position.Set(x, y);
    bodyDefinition.angle = angle;
    bodyDefinition.active = false;

    this->shape = new b2PolygonShape();
    b2Vec2 vertices[4];

    // First vertex.
    vertices[0].Set(0.35f, -0.25f);

    // Second vertex.
    float cosinus = cosf(arc / 2.0f);
    float sinus = sinf(arc / 2.0f);
    vertices[1].Set(0.35f + length, -0.25f - length * sinus / cosinus);

    // Third vertex.
    vertices[2].Set(0.35f + length, 0.25f + length * sinus / cosinus);
    this->base = (vertices[2].y - vertices[1].y) / 2.0f;

    // Fourth vertex.
    vertices[3].Set(0.35f, 0.25f);

    this->shape->Set(vertices, 4);

    this->body = world->CreateBody(&bodyDefinition);
    this->body->CreateFixture(this->shape, 0.0f);
}

bool LightElement::isLight() {
    return true;
}
